package com.fary.net;

import com.fary.net.NioEndpoint.Poller;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.channels.SocketChannel;


public class NioChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel {

    protected SocketChannel sc = null;
    protected SocketWrapperBase<NioChannel> socketWrapper = null;

    protected final SocketBufferHandler bufHandler;

    protected Poller poller;

    public NioChannel(SocketChannel channel, SocketBufferHandler bufHandler) {
        this.sc = channel;
        this.bufHandler = bufHandler;
    }

    public void setPoller(Poller poller) {
        this.poller = poller;
    }

    public Poller getPoller() {
        return poller;
    }

    public SocketChannel getIOChannel() {
        return sc;
    }

    public SocketBufferHandler getBufHandler() {
        return bufHandler;
    }

    void setSocketWrapper(SocketWrapperBase<NioChannel> socketWrapper) {
        this.socketWrapper = socketWrapper;
    }


    @Override
    public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
        return 0;
    }

    @Override
    public long write(ByteBuffer[] srcs) throws IOException {
        return 0;
    }

    @Override
    public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
        return 0;
    }

    @Override
    public long read(ByteBuffer[] dsts) throws IOException {
        return 0;
    }

    @Override
    public int read(ByteBuffer dst) throws IOException {
        return 0;
    }

    @Override
    public int write(ByteBuffer src) throws IOException {
        return 0;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    public void close(boolean force) throws IOException {
        if (isOpen() || force ) {
            close();
        }
    }

    @Override
    public void close() {
        try {
            getIOChannel().socket().close();
            getIOChannel().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setIOChannel(SocketChannel IOChannel) {
        this.sc = IOChannel;
    }

    public void reset() throws IOException {
        bufHandler.reset();
    }
}